// pages/school/plan/index.js
const {
  schoolNewsPage,
  schoolNews,
  schoolJoList
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 9,
    pageNo: 1,
    pageSize: 20,
    type: 'word',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        type: options.type || 'word'
      })
    }
    if (options.type == 'word') {
      this.getSchoolNewsPage()
    } else {
      wx.setNavigationBarTitle({
        title: '就业质量',
      })
      this.schoolJoList()
    }
  },


  // 获取招生快讯
  async getSchoolNewsPage() {
    const {
      pageNo,
      pageSize,
      id
    } = this.data
    const params = {
      pageNo,
      pageSize,
      schoolId: id
    }
    const {
      result
    } = await schoolNewsPage(params)
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },
  // 学校就业质量报告
  async schoolJoList() {
    const {
      id
    } = this.data
    const pageNo = 1
    const {
      result
    } = await schoolJoList(id)
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      type
    } = this.data
    if (type == 'pdf') return
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) return
    this.getSchoolNewsPage()
  },


  async detail(e) {
    const {
      id,
      path
    } = e.currentTarget.dataset
    const {
      type
    } = this.data
    if (type == 'word') {
      const {
        result
      } = await schoolNews(id)
      wx.navigateTo({
        url: `/pages/article/rich-text/index?content=` + encodeURIComponent(result.content) + '&name=' + result.title + '&type=parse',
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.downloadFile({
        url: path,
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            showMenu: true,
            fileType: 'pdf',
            success: function (res) {
              wx.hideLoading()
            }
          })
        }
      })
      // wx.navigateTo({
      //   url: '/pages/web/index?url=' + path,
      // })
    }
  },
  onShareAppMessage: function () {
    const {
      id,type
    } = this.data
    return {
      title: type=='word'?'招生计划':'就业质量',
      path: `/pages/school/plan/index?id=${id}&type=${type}`,
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  },
})